/********************************************************
* @file: WINZOOM.cpp
* @date: 09/09/2009
********************************************************
* acmFrame
********************************************************/

// includes
//----------
#include <stdlib.h> 
#include "winzoom.h"


// initializations
//-----------------
WINZOOM* WINZOOM::m_zoom = NULL;


//
//////////////////////////////////////////////////////////////////////////
WINZOOM::WINZOOM(): 
	m_active     ( false )
, m_objdraw    ( NULL )
, m_endAction  ( NULL )
, m_firstPts ( false )
, m_oldState   ( -1 )
{
}

//
//////////////////////////////////////////////////////////////////////////
WINZOOM::~WINZOOM()
{
//  if( m_objdraw != NULL )
//    delete m_objdraw;
}


//
//////////////////////////////////////////////////////////////////////////
WINZOOM&
WINZOOM::instance()
{
  if( !m_zoom )
  {
    m_zoom = new WINZOOM();
  }
  return *m_zoom;
}


//
//////////////////////////////////////////////////////////////////////////
void
 WINZOOM::setEndAction(void *obj, void (*endAction)(void *obj, double xmin, double ymin, double xmax, double ymax, int old_state))
{
  m_endAction = endAction;
  m_objClass  = obj;
}

//
//////////////////////////////////////////////////////////////////////////
void 
WINZOOM::setDraw ( drawGRA *_obj )
{
  m_objdraw = _obj;
}

//
//////////////////////////////////////////////////////////////////////////
int
WINZOOM::begin (int curr_state)
{
	if (!m_active)
		return 0;
	
  m_firstPts = false;
  m_oldState = curr_state;
  
  return 1;
}

//
//////////////////////////////////////////////////////////////////////////
int 
WINZOOM::addPoint( double _x, double _y)
{
	if (!m_active)
		return 0;
  
  if (!m_firstPts)
  {
  	m_x1 = xtmp = _x; 
  	m_y1 = ytmp = _y;
    m_firstPts = true;
  }
  else  // set second point
  {
  	m_x2 = _x; 
  	m_y2 = _y;

    // compute maximum and minimum values
    double xmin, ymin, xmax, ymax;
    xmin = xmax = m_x2;
    ymin = ymax = m_y2;
    
    if (m_x1 > m_x2)
      xmax = m_x1;
    else
      xmin = m_x1;
    
    if (m_y1 > m_y2)
      ymax = m_y1;
    else
      ymin = m_y1;
    
    m_endAction(m_objClass, xmin, ymin, xmax, ymax, m_oldState); // create curve

    m_oldState = -1;
    m_firstPts = m_active = false;

  }

  return 1;
}

//
//////////////////////////////////////////////////////////////////////////
int 
WINZOOM::addTempPoint( double _x, double _y )
{
	if (!m_active)
		return 0;

  xtmp = _x;
  ytmp = _y;

  return 1;
}


//
//////////////////////////////////////////////////////////////////////////
void 
WINZOOM::draw()
{
	if (!m_active)
		return;

  // compute maximum and minimum values
  if (m_firstPts && m_objdraw != NULL)
  {
    m_objdraw->SetLineColor (draw_CYAN);

    m_objdraw->begin ( );
    m_objdraw->addVertex (m_x1, m_y1, 0);
    m_objdraw->addVertex (xtmp, m_y1, 0);
    m_objdraw->end ( );

    m_objdraw->begin ( );
    m_objdraw->addVertex (m_x1, ytmp, 0);
    m_objdraw->addVertex (xtmp, ytmp, 0);
    m_objdraw->end ( );

    m_objdraw->begin ( );
    m_objdraw->addVertex (m_x1, m_y1, 0);
    m_objdraw->addVertex (m_x1, ytmp, 0);
    m_objdraw->end ( );

    m_objdraw->begin ( );
    m_objdraw->addVertex (xtmp, m_y1, 0);
    m_objdraw->addVertex (xtmp, ytmp, 0);
    m_objdraw->end ( );
  }
}

